package cn.e4j.config;

import cn.e4j.core.util.FileUtil;
import cn.e4j.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;

import javax.servlet.MultipartConfigElement;

/**
 * @author wenchuan.li
 * @date 2019/1/14 16:55
 */
@Configuration
public class MultipartConfig {

    @Value("${e4j.file-upload-path}")
    private String uploadFolder;

    @Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        String rootPath = ToolUtil.getWebRootPath(null);
        rootPath = rootPath.substring(0, rootPath.lastIndexOf("/"));
        FileUtil.createFolder(rootPath + "/" + uploadFolder, false);
        factory.setLocation(rootPath + "/" + uploadFolder);
        //文件最大
        factory.setMaxFileSize(DataSize.ofMegabytes(50));
        // 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofMegabytes(50));
        return factory.createMultipartConfig();
    }

}
